import allure,random,datetime,time,openpyxl,pykeyboard,pyperclip,timedelta
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class BaseCommon(object):
    def __init__(self, driver):
        super().__init__(driver) # 继承BaseAction公用方法
    # def __init__(self, driver):
    #     self.driver = driver  # 示例化驱动
    #     self.time = time
    #     self.wait = WebDriverWait(self.driver, 20)  # 设置浏览器：driver 等待时间：20s,
        # WebDriverWait(driver, 超时时长, 调用频率, 忽略异常).until(可执行方法, 超时时返回的信息)


    # 生成随机姓名
    @allure.step(title="操作步骤:生成随机姓名")
    def create_name(self):
        """
        :rtype: object
        """
        firstname = ["李","王","张","刘","陈","杨","黄","赵","周","吴","徐","孙","朱","马","胡","郭","林","何","高","梁","郑","罗","宋","谢","唐","韩","曹","许","邓","萧","冯","曾","程","蔡","彭","潘","袁","於","董","余","苏","叶","吕","魏","蒋","田","杜","丁","沈","姜","范","江","傅","钟","卢","汪","戴","崔","任","陆","廖","姚","方","金","邱","夏","谭","韦","贾","邹","石","熊","孟","秦","阎","薛","侯","雷","白","龙","段","郝","孔","邵","史","毛","常","万","顾","赖","武","康","贺","严","尹","钱","施","牛","洪","龚"]
        Blastname = ["豪","言","玉","意","泽","彦","轩","景","正","程","诚","宇","澄","安","青","泽","轩","旭","恒","思","宇","嘉","宏","皓","成","宇","轩","玮","桦","宇","达","韵","磊","泽","博","昌","信","彤","逸","柏","新","劲","鸿","文","恩","远","翰","圣","哲","家","林","景","行","律","本","乐","康","昊","宇","麦","冬","景","武","茂","才","军","林","茂","飞","昊","明","明","天","伦","峰","志","辰","亦"]
        Glastname = [chr(random.randint(0x4E00, 0x9FBF)),"佳","彤","自","怡","颖","宸","雅","微","羽","馨","思","纾","欣","元","凡","晴","玥","宁","佳","蕾","桑","妍","萱","宛","欣","灵","烟","文","柏","艺","以","如","雪","璐","言","婷","青","安","昕","淑","雅","颖","云","艺","忻","梓","江","丽","梦","雪","沁","思","羽","羽","雅","访","烟","萱","忆","慧","娅","茹","嘉","幻","辰","妍","雨","蕊","欣","芸","亦"]
        return random.choice(firstname)+random.choice(Blastname)+random.choice(Glastname)

    @allure.step(title="操作步骤:生成随机手机号")
    def create_phone(self):
        """
        :rtype: object
        """
        # 获取时间戳截取后五位
        now = int(time.time())
        now_timestamp2 = str(now)[2:]
        first = ["181", "180", "134", "159","132"]
        phone = random.choice(first) + now_timestamp2
        return phone

    @allure.step(title="操作步骤:生成随机证件号")
    def create_card(self):
        """
        :rtype: object
        """
        date2 = datetime.datetime.now().date().isoformat().split("-")  # 获取当前日期并转换成字符串格式
        current_date = "".join(date2)
        # 获取时间戳截取后五位
        now = int(time.time())
        now_timestamp1 = str(now)[5:]
        card_no = current_date + now_timestamp1
        return card_no

    @allure.step(title="操作步骤:生成随机身份证证件号")
    def generate_id(self, sex=0):
        """ 随机生成身份证号，sex = 0表示女性，sex = 1表示男性 """




    @allure.step(title="操作步骤:传入学员信息，自动生成xlsx导入学员文件")
    def generateStu(self,file_path1, stu_name, card_type, card_no, phone, term, level, major, xbie,mzu,csheng,xxzx,
                    data_quantity, file_path2=""):
        """
        传入学员信息，自动生成xlsx导入学员文件
        :param file_path1:文件路径
        :param stu_name:学员姓名
        :param card_type:证件类型
        :param card_no:证件号码
        :param phone:手机号
        :param term:入学学期
        :param level:层次
        :param major:专业
        :param biz_owner_id:业务归属人工号
        :param biz_owner_name:业务归属人姓名
        :param data_quantity:需要的数据量
        :param file_path2:另存为你指定的路径
        :return:
        """
        wb = openpyxl.load_workbook(file_path1)
        sheet = wb["导入录取学员"]

        for i in range(2, data_quantity + 2):
            sheet["A" + str(i)].value = stu_name
            sheet["B" + str(i)].value = card_type
            sheet["C" + str(i)].value = str(card_no)
            sheet["D" + str(i)].value = str(phone)
            sheet["E" + str(i)].value = term
            sheet["F" + str(i)].value = level
            sheet["G" + str(i)].value = major
            sheet["H" + str(i)].value = xbie
            sheet["I" + str(i)].value = mzu
            sheet["J" + str(i)].value = csheng
            sheet["K" + str(i)].value = xxzx
        if file_path2 != "":
            wb.save(file_path2)
        wb.save(file_path1)

    @allure.step(title="操作步骤:传入学员信息，自动生成学员并导入文件")
    def generate_student(self,m_filpath,stu_name,card_type,term,level,major,xbie,mzu,csheng,xxzx):
        date2 = datetime.datetime.now().date().isoformat().split("-")  # 获取当前日期并转换成字符串格式
        current_date = "".join(date2)
        # 获取时间戳截取后五位
        now = int(time.time())
        now_timestamp1 = str(now)[5:]
        now_timestamp2 = str(now)[2:]
        card_no = current_date + now_timestamp1
        phone = "180" + now_timestamp2
        self.generateStu(m_filpath,stu_name,card_type,card_no,phone,term,level,major,xbie,mzu,csheng,xxzx,1)
        return stu_name,phone

    @allure.step(title="操作步骤:传入文件绝对路径，确认文件上传")
    def upload_file(self, filepath):
        """
        :rtype: object
        """
        #浏览器原生，调用上传文件
        time.sleep(2)
        k = pykeyboard.PyKeyboard()#实例化
        pyperclip.copy(filepath)#复制带中文路径的文件地址
        time.sleep(2)
        k.press_key(k.control_key)#键盘操作黏贴
        k.tap_key('v')
        k.release_key(k.control_key)
        k.tap_key(k.tab_key)#tab到下一个框
        k.tap_key(k.enter_key) # 回车键确认上传
        time.sleep(1)

    @allure.step(title="操作步骤:生成指定数以内的随机数")
    def create_number(self,num):

        return random.randint(1,num)

    @allure.step(title="操作步骤:删除已有字符")
    def press_back(self,num):
        """
        :rtype: object
        """
        k = pykeyboard.PyKeyboard()  # 实例化
        k.tap_key(k.end_key)
        for i in range(num):
            k.tap_key(k.backspace_key)

    @allure.step(title="操作步骤:选择下拉第一个")
    def choose_1st_select(self):
        """
        :rtype: object
        """
        k = pykeyboard.PyKeyboard()  # 实例化
        k.tap_key(k.down_key)   # 按向下的方向键
        k.tap_key(k.enter_key)  # 按住enter键


    @allure.step(title="操作步骤:选择下拉倒数第一个")
    def choose_1ist_select1(self):
        """
        :rtype: object
        """
        k = pykeyboard.PyKeyboard()  # 实例化
        k.tap_key(k.up_key)   # 按向上的方向键
        k.tap_key(k.enter_key)  # 按住enter键



    @allure.step(title="操作步骤:修改订单模板，并保存文件")
    def generateStu1(self,file_path2=""):
        """
        传入学员信息，自动生成xlsx导入学员文件
        :param file_path1:文件路径
        :param stu_account:学员账号
        :param card_no:证件号码
        :param phone:手机号
        :param data_quantity:需要的数据量
        :param file_path2:另存为你指定的路径
        :return:
        """
        file_path = 'F:\\test4hjx_glht\\data\\订单批量导入模板.xlsx'
        wb = openpyxl.load_workbook(file_path)     # 读取工作簿
        worksheet1 = wb[wb.sheetnames[0]]   # 读取第一个功表

        date2 = datetime.datetime.now().date().isoformat().split("-")  # 获取当前日期并转换成字符串格式
        current_date = "".join(date2)
        # 获取时间戳截取后五位
        now = int(time.time())
        now_timestamp1 = str(now)[5:]
        now_timestamp2 = str(now)[2:]

        card_no = current_date + now_timestamp1
        phone = "180" + now_timestamp2
        stu_account = "98980" + now_timestamp2
        for i in range(2, 3):
            # worksheet1.cell(i, 1).value = stu_account    # 如系统是线下收费，就需要学员账号
            worksheet1.cell(i, 3).value = phone
            worksheet1.cell(i, 5).value = card_no

        if file_path2 != "":
            wb.save(file_path2)
        wb.save(file_path)

    @allure.step(title="操作步骤:传入学员信息，自动生成学员并导入文件")
    def generate_student1(self):
        date2 = datetime.datetime.now().date().isoformat().split("-")  # 获取当前日期并转换成字符串格式
        current_date = "".join(date2)
        # 获取时间戳截取后五位
        now = int(time.time())
        now_timestamp1 = str(now)[5:]
        now_timestamp2 = str(now)[2:]

        card_no = current_date + now_timestamp1
        phone = "180" + now_timestamp2
        stu_account = "98980" + now_timestamp2
        self.generateStu1(stu_account,card_no,phone,)
        return stu_account,phone,card_no

